package com.ruoyi.gateway;

import io.netty.buffer.UnpooledByteBufAllocator;
import org.apache.commons.compress.parallel.InputStreamSupplier;
import org.apache.commons.io.input.NullInputStream;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class FluxTest {
    public static void main(String[] args) {
        AtomicInteger ai = new AtomicInteger(0);
        DefaultDataBufferFactory defaultDataBufferFactory = new DefaultDataBufferFactory();
        final Callable<InputStream> inputStreamSupplier = () -> {
            try {
                //return new ByteArrayInputStream("aaaa".getBytes(StandardCharsets.UTF_8));
                return new FileInputStream(new File("C:\\Users\\xuykj\\Desktop\\allsp.txt"));
            } catch (Exception e) {
                e.printStackTrace();
                return new NullInputStream(0);
            }
        };
        StringBuffer sb = new StringBuffer();
        Flux<DataBuffer> ff = DataBufferUtils.readInputStream(inputStreamSupplier, defaultDataBufferFactory, 512);
        ff.subscribe(buffer -> {
            //buffer =每个缓存的大小，循环
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
            DataBufferUtils.release(buffer);
            ai.getAndIncrement();
            sb.append(charBuffer.toString());
        });
        Mono.just(1).subscribe(d -> System.out.println(d));
        System.out.println(sb.toString());
        System.out.println(ai.get());
    }
}
